package br.com.j7.jdesk.ui.bean;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.primefaces.model.chart.PieChartModel;

import br.com.j7.jdesk.persistence.entity.Services;
import br.com.j7.jdesk.persistence.entity.ServicesSituations;
import br.com.j7.jdesk.persistence.entity.ServicesTypes;
import br.com.j7.jdesk.persistence.repository.ServiceRepository;
import br.com.j7.jdesk.persistence.repository.ServiceSituationRepository;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepository;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepositoryTest;
import br.com.j7.jdesk.persistence.repository.impl.ServiceRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceSituationRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceTypeRepositoryImpl;

@ManagedBean
@RequestScoped
public class ResumoBean extends WebContext{

	private List<Services> services = new ArrayList<>();
	
	private PieChartModel pieServiceSituation;
	private PieChartModel pieServiceType;
	private ServiceRepository serviceRepository;
	private ServiceSituationRepository serviceSituationRepository;
	private ServiceTypeRepository serviceTypeRepository;
	
	private Map<String, Integer> mapSituationCount = new LinkedHashMap<>();
	private Map<String, Integer> mapServiceTypeCount = new LinkedHashMap<>();

	public ResumoBean() {
		serviceSituationRepository = new ServiceSituationRepositoryImpl();
		serviceTypeRepository = new ServiceTypeRepositoryImpl();
		serviceRepository = new ServiceRepositoryImpl();
		
		obterService();
		agruparServiceSituation();
		agruparServiceType();
		createPieServiceSituation();
		createPieServiceType();
	}

	private void createPieServiceSituation() {
		pieServiceSituation = new PieChartModel();
		for(Map.Entry<String, Integer>  map : mapSituationCount.entrySet()){
			pieServiceSituation.set(map.getKey(), map.getValue());
		}
	}
	
	private void createPieServiceType() {
		pieServiceType = new PieChartModel();
		for(Map.Entry<String, Integer>  map : mapServiceTypeCount.entrySet()){
			pieServiceType.set(map.getKey(), map.getValue());
		}
	}

	public PieChartModel getPieServiceSituation() {
		return pieServiceSituation;
	}
	
	public PieChartModel getPieServiceType() {
		return pieServiceType;
	}
	
	private void agruparServiceSituation(){
		for (Services s : services) {
			for(ServicesSituations ss : listServiceSituation()){
				if(s.getServiceSituation().getCdServiceSituation().equalsIgnoreCase(ss.getCdServiceSituation())){
					if(!mapSituationCount.containsKey(ss.getCdServiceSituation())){
						mapSituationCount.put(ss.getCdServiceSituation(), 1);
					}else{
						mapSituationCount.put(ss.getCdServiceSituation(), mapSituationCount.get(ss.getCdServiceSituation())+1);
					}
				}
			}
		}	
	}
	
	private void agruparServiceType() {
		for (Services s : services) {
			for(ServicesTypes ss : listServiceType()){
				if(s.getServiceType().getCdServiceType().equalsIgnoreCase(ss.getCdServiceType())){
					if(!mapServiceTypeCount.containsKey(ss.getCdServiceType())){
						mapServiceTypeCount.put(ss.getCdServiceType(), 1);
					}else{
						mapServiceTypeCount.put(ss.getCdServiceType(), mapServiceTypeCount.get(ss.getCdServiceType())+1);
					}
				}
			}
		}	
		
	}
	
	private List<ServicesSituations> listServiceSituation(){
		try {
			return serviceSituationRepository.listByEnterprise(getSessionEnterprise());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private List<ServicesTypes> listServiceType(){
		try {
			return serviceTypeRepository.listByEnterprise(getSessionEnterprise());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private void obterService() {
		try {
			services = serviceRepository.listByEnterprise(getSessionEnterprise());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
